-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

-- Random-walk
newAI("move_random", function(self)
	local possibilities = {}
	for x=self.x-1,self.x+1 do for y=self.y-1,self.y+1 do
		if not game.level.map:checkAllEntities(x, y, "block_move", self, true) then
			table.insert(possibilities, {x=x, y=y})
		end
	end end
	if #possibilities == 0 then return end
	local dest = rng.table(possibilities)
	return self:move(dest.x, dest.y)
end)

-- Check if the current target is hostile, and if not lose it
newAI("target_simple_check_hostility", function(self)
    if self.ai_target.actor and self:reactionToward(self.ai_target.actor) >= 0 then self.ai_target.actor = nil end
	return self:runAI("target_simple")
end)

newAI("dumb_talented_simple_wander", function(self)
	if self:runAI(self.ai_state.ai_target or "target_simple_check_hostility") then
		-- One in "talent_in" chance of using a talent
		if rng.chance(self.ai_state.talent_in or 6) then
			self:runAI("dumb_talented")
		end
		if not self.energy.used then
			self:runAI(self.ai_state.ai_move or "move_dmap")
		end
		return true
	else
		return self:runAI(self.ai_state.ai_wander or "move_random")
	end
end)